package com.lemon.cloud.generator.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lemon.cloud.generator.entity.GenConfig;
import com.lemon.cloud.generator.entity.GenFormConf;
import com.lemon.cloud.generator.mapper.GenDatasourceConfMapper;
import com.lemon.cloud.generator.mapper.GenFormConfMapper;
import com.lemon.cloud.generator.service.GeneratorService;
import com.lemon.cloud.generator.utils.CodeGenKits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

@Service
public class GeneratorServiceImpl implements GeneratorService {

    @Autowired
    private GenFormConfMapper genFormConfMapper;

    @Autowired
    private GenDatasourceConfMapper genDatasourceConfMapper;

    @Override
    public byte[] generatorCode(GenConfig genConfig) {
        // 根据tableName 查询最新的表单配置
        List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
                .eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        String tableNames = genConfig.getTableName();
        for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
            // 查询表信息
            Map<String, String> table = genDatasourceConfMapper.queryTable(tableName, genConfig.getDsName());
            // 查询列信息
            List<Map<String, String>> columns = genDatasourceConfMapper.queryColumns(tableName, genConfig.getDsName());
            // 生成代码
            if (CollUtil.isNotEmpty(formConfList)) {
                CodeGenKits.generatorCode(genConfig, table, columns, zip, formConfList.get(0));
            } else {
                CodeGenKits.generatorCode(genConfig, table, columns, zip, null);
            }
        }
        IoUtil.close(zip);
        return outputStream.toByteArray();
    }


    @Override
    public Map<String, String> previewCode(GenConfig genConfig) {
        // 根据tableName 查询最新的表单配置
        List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
                .eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));

        String tableNames = genConfig.getTableName();
        for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
            // 查询表信息
            Map<String, String> table = genDatasourceConfMapper.queryTable(tableName, genConfig.getDsName());
            // 查询列信息
            List<Map<String, String>> columns = genDatasourceConfMapper.queryColumns(tableName, genConfig.getDsName());
            // 生成代码
            if (CollUtil.isNotEmpty(formConfList)) {
                return CodeGenKits.generatorCode(genConfig, table, columns, null, formConfList.get(0));
            }
            else {
                return CodeGenKits.generatorCode(genConfig, table, columns, null, null);
            }
        }

        return MapUtil.empty();
    }
}
